package com.example.demo.practice.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.example.demo.practice.dashboardview.PxUtils;
import com.example.demo.practice.tool.TimeAction;

/**
 * Created by hanbo on 2018-01-23.
 */

public class SatelliteView extends View {
    private static String TAG = "卫星图";
    private int mWidth;
    private int mHeight;
    private Paint p;
    private Context context;
    //定时器
    private TimeAction timeAction;
    //运动圆的轨迹
    private float x;
    private float y;
    private float x2;
    private float y2;
    //圆心坐标
    private int x1;
    private int y1;
    //最大半径
    private int r1;
    //r平方
    private int r_run;
    private int r_run2;
    //速度
    private static double speed = 0.01;
    private static double speed2 = 0.006;
    private double degree;
    private double degree2;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            degree = speed + degree;
            if (degree >= 360) {
                degree = degree - 360;
            }
            degree2 = speed2 + degree2;
            if (degree2 >= 360) {
                degree2 = degree2 - 360;
            }

            y = (float) (y1 - r_run * Math.cos(degree));
            x = (float) (x1 + r_run * Math.sin(degree));
            y2 = (float) (y1 - r_run2 * Math.cos(degree2));
            x2 = (float) (x1 + r_run2 * Math.sin(degree2));


            invalidate();
        }
    };

    public SatelliteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        p = new Paint();
        p.setAntiAlias(true);
        degree = 0;
        timeAction = new TimeAction(handler, 40);
        timeAction.start();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            mWidth = PxUtils.dpToPx(200, context);
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else {
            mHeight = PxUtils.dpToPx(200, context);
        }
        setMeasuredDimension(mWidth, mHeight);

        r1 = mWidth;
        if (mHeight < mWidth) {
            r1 = mHeight;
        }
        r_run = r1 / 4;
        x = mWidth / 2;
        x1 = mWidth / 2;
        y1 = mHeight / 2;
        y = y1 - r_run;

        x2 = mWidth / 2;
        r_run2 = r1 * 3 / 8;
        y2 = y1 - r_run2;


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        p.setColor(Color.parseColor("#00a1e9"));
        p.setStrokeWidth(5);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(x1, y1, r1 / 4, p);
        p.setColor(Color.parseColor("#106391"));
        canvas.drawCircle(x1, y1, r1 * 5 / 16, p);
        p.setColor(Color.parseColor("#194464"));
        canvas.drawCircle(x1, y1, r1 * 3 / 8, p);
        p.setColor(Color.parseColor("#1e324a"));
        canvas.drawCircle(x1, y1, r1 * 7 / 16, p);
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.parseColor("#00a1e9"));
        canvas.drawCircle(x, y, 15, p);
        canvas.drawCircle(x2, y2, 10, p);
    }

    @Override
    protected void onDetachedFromWindow() {
        timeAction.stop();
        super.onDetachedFromWindow();
    }
}
